import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import Editor from 'for-editor';
import addQuestion from "@/services/addQuestion";

export default class EditMarkdown extends Component {
    state = {
        value:''
    }

    handleChange = (value) => {
        this.setState({
            value
        })
    }

    //添加图片
    addImg = async (file) => {
        console.log(file);
        let formdata = new FormData();
        formdata.append('file',file);
        let res = await addQuestion.uploadImage(formdata);
        console.log("上传图片res",res);
        this.setState({
            value:`${this.state.value}![alt](${process.env.IMAGE_PATH + res.filename})`
        })
        
    }

    save = (value) => {
        this.setState({
            value
        })
    }

    render() {
        let {value } = this.state;
        return (
            <div>
                <Editor 
                    value={value} 
                    onChange={this.handleChange.bind(this)} 
                    addImg={this.addImg}
                    onSave={this.save}
                />
            </div>
        )
    }
}



